home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / mktil16a.zip / HEADERS.ZIP / PALETTE.H < prev    next >
C/C++ Source or Header  |  1994-09-16  |  3KB  |  171 lines

  1. #define MAXCOLORS 256
  2.  
  3. #ifndef d_word
  4.  #define d_word unsigned long
  5. #endif
  6. #ifndef word
  7.  #define word unsigned int
  8. #endif
  9. #ifndef byte
  10.  #define byte unsigned char
  11. #endif
  12.  
  13. #ifndef PALETTE_TIMER
  14.  #define PALETTE_TIMER 250
  15. #endif
  16.  
  17. #include <sys\timeb.h>
  18.  
  19. struct timeb time_pointer;
  20. d_word old_palette_timer, palette_timer=0, max_palette_timer=PALETTE_TIMER;
  21. byte palette[768];
  22. byte palette_flag=OFF;
  23.  
  24. byte read_palette(char *filename)
  25. {
  26.  char buf[80];
  27.  size_t itemsread;
  28.  FILE *file;
  29.  
  30.  sprintf(buf,"%s%s.pal",path,filename);
  31.  
  32.  if ((file=fopen(buf,"rb"))==NULL)
  33.  {
  34.   printf("Unable to open %s for read.\n",buf);
  35.   return (FAILURE);
  36.  }
  37.  itemsread=fread(palette,3,MAXCOLORS,file);
  38.  fclose(file);
  39.  
  40.  return(SUCCESS);
  41. }
  42.  
  43. byte write_palette(char *filename)
  44. {
  45.  size_t itemswritten;
  46.  FILE *file;
  47.  char buf[80];
  48.  
  49.  sprintf(buf,"%s%s.pal",path,filename);
  50.  
  51.  if ((file=fopen(buf,"wb"))==NULL)
  52.  {
  53.   printf("Unable to open palette file for write.");
  54.   return (FAILURE);
  55.  }
  56.  itemswritten=fwrite(palette,3,MAXCOLORS,file);
  57.  fclose(file);
  58.  return(SUCCESS);
  59. }
  60.  
  61. void set_palette(void)
  62. {
  63.  union REGS regs;
  64.  struct SREGS segs;
  65.  
  66.  char *pal=palette;
  67.  
  68.  regs.h.ah=0x10;
  69.  regs.h.al=0x12;
  70.  
  71.  regs.x.bx=0;               // First color
  72.  regs.x.cx=256;             // Number of colors
  73.  segs.es=FP_SEG(pal);
  74.  regs.x.dx=FP_OFF(pal);
  75.  
  76.  _asm
  77.  {
  78.         cli                     /* clear interrupts */
  79.         mov       dx,03DAh      /* wait for vert. retrace */
  80.   start:
  81.         in        al,dx
  82.         and       al,08h
  83.         jnz       start
  84.   mid:
  85.         in        al,dx
  86.         and       al,08h
  87.         jz        mid
  88.  
  89.         sti                     /* restore interrupts */
  90.  }
  91.  
  92.  palette_flag=ON;                       /* so mouse won't interrupt */
  93.  int86x(0x10,®s,®s,&segs);
  94.  palette_flag=OFF;                      /* re-enable mouse (see mouse.h) */
  95. }
  96.  
  97. void palette_switch(byte start, byte amount)
  98. {
  99.  union REGS regs;
  100.  struct SREGS segs;
  101.  int a, b;
  102.  byte tmp_col[3];
  103.  char *pal=palette;
  104.  
  105.  /* wait PALETTE_DELAY milliseconds before rotating palette */
  106.  
  107.  old_palette_timer=(d_word)(time_pointer.time*1000+time_pointer.millitm);
  108.  ftime(&time_pointer);
  109.  palette_timer+=(d_word)((time_pointer.time*1000+time_pointer.millitm)
  110.            -old_palette_timer);
  111.  
  112.  if (palette_timer>=max_palette_timer)
  113.   max_palette_timer=(d_word)(palette_timer+PALETTE_TIMER);
  114.  else return;
  115.  
  116.  /* rotate the palette */
  117.  
  118.  for (a=start;a<start+amount;a+=4)
  119.  {
  120.   memcpy(tmp_col,&palette[a*3+0],3);
  121.  
  122.   for (b=0;b<3;b++)
  123.   {
  124.    memcpy(&palette[(a+b)*3],&palette[(a+b+1)*3],3);
  125.   }
  126.  
  127.   memcpy(&palette[(a+3)*3],tmp_col,3);
  128.  }
  129.  
  130.  /* set up the registers now for the palette setting interrupt */
  131.  
  132.   regs.h.ah=0x10;
  133.   regs.h.al=0x12;
  134.  
  135.   regs.x.bx=start;               // First color
  136.   regs.x.cx=amount;             // Number of colors
  137.   segs.es=FP_SEG(pal);
  138.   regs.x.dx=FP_OFF(pal)+start*3;
  139.  
  140.  _asm
  141.  {
  142.         cli
  143.         mov       dx,03DAh              /* wait for vert. retrace */
  144.   begin:
  145.         in        al,dx
  146.         and       al,08h
  147.         jnz       begin
  148.   mid:
  149.         in        al,dx
  150.         and       al,08h
  151.         jz        mid
  152.  
  153.         sti
  154.  }
  155.  
  156.  palette_flag=ON;                       /* disable the mouse */
  157.  set_palette();
  158. // int86x(0x10,®s,®s,&segs);
  159.  palette_flag=OFF;                      /* re-enable mouse (see mouse.h) */
  160. }
  161.  
  162. void adjust_palette(byte shift)
  163. {
  164.  word a, b;
  165.  
  166.  for (a=0;a<768;a++)
  167.   palette[a]=palette[a]>>shift;
  168. }
  169.  
  170.  
  171.